package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.ExchangeConstants;
import com.alimama.mobile.sdk.config.MMPromoter;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.alimama.mobile.sdk.config.WelcomeAdsListener;
import com.alimama.mobile.sdk.config.WelcomeProperties;
import com.xm9m.ibczy.mytest.R;

public class WelcomeActivity extends Activity {
	ImageView frame;
	private MmuSDK mmuSDK;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		frame = new ImageView(this);
		frame.setLayoutParams(new LayoutParams(-1, -1));
		frame.setImageResource(R.drawable.welcome_splash);
		frame.setScaleType(ScaleType.FIT_XY);
		setContentView(frame);
		ExchangeConstants.WELCOME_COUNTDOWN = true;// 设置是否显示开屏样式倒计时

		String slotId = "54366";// 注意：该广告位只做测试使用，请勿集成到发布版app中
		setupAlimama(this, slotId);

	}

	/**
	 * 开屏样式集成： WelcomeAdsListener：无论是否展示都会调用onFinish 超时或请求数据完毕 | onDataReviced
	 * |-没有广告-onFinish 有广告 | onShow |-发生错误-onError | | onCountdown onFinish |
	 * onFinish 如果在广告加载期间启动页已经关闭，将不会回调。
	 */
	private void setupAlimama(Context context, String slotId) {
		mmuSDK = MmuSDKFactory.getMmuSDK();
		mmuSDK.accountServiceInit(this);
		mmuSDK.init(getApplication());// 初始化SDK,该方法必须保证在集成代码前调用，可移到程序入口处调用
		// 广告将在调用addview 1000~3000
		// 内展示，如果该时间段内没有接收到广告数据将调用onFinish
		WelcomeProperties properties = new WelcomeProperties(context, slotId,
				1000, 3000, new WelcomeAdsListener() {

					@Override
					public void onShow(View arg0) {

					}

					@Override
					public void onFinish() {
						frame.setImageResource(R.drawable.welcome_content);
					}

					@Override
					public void onError(String arg0) {

					}

					@Override
					public void onDataReviced(MMPromoter arg0) {

					}

					@Override
					public void onCountdown(int arg0) {

					}
				});
		
		/**
		 * 以下代码开发者可以自行传入开屏容器，容器layout参考container_welcome.xml，
		 * 如果传入容器，则其中必须包含id为taobao_layout_iv的viewgroup 
		**/
		
		ViewGroup handleContainer = (ViewGroup) getLayoutInflater().inflate(
				R.layout.container_welcome, null);
		properties.setWelcomeContainer(handleContainer);
		
		//如果调用失败需要自行跳转到主界面
		boolean success = mmuSDK.attach(properties);
		if (!success) {
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					frame.setImageResource(R.drawable.welcome_content);
				}
			}, 1500);
			
		}
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (!mmuSDK.accountServiceHandleResult(requestCode, resultCode, data,this)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
    }
}
